/**
 * @class   wosh::gui::BusRingQt
 * @brief   Bus implementation, BusRing architecture.
 *          Messages are posted asynchronously.
 *
 ****************************************************************************
 * @version $Id: BusRingQt.h 2184 2010-03-20 13:19:23Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_BusRingQt_H__
 #define __WOSH_Core_BusRingQt_H__

 #include <woshDefs.h>
 #include <core/Bus.h>
 #include <core/MessageQueue.h>

 #include <QObject>


namespace wosh {
 namespace gui {


 #define _BusRingQt_NAME			"wosh.Bus.RingQt"
 #define _BusRingQt_TYPE			"wosh::BusRingQt"


class BusRingQt :		protected QObject,
						public Bus
 {
	Q_OBJECT

	WOSH_BUS(wosh::gui::BusRingQt)

	public:

		/**
		 * @brief  Empty constructor.
		 * @param [in] busname the name to assign to the bus (usually it's something like 'wosh.Bus.Devices')
		 * @note   Raise the BusManager created (1) event.
		 */
		BusRingQt( const std::string& busname = "" );

		/**
		 * @brief  Empty destructor.
		 * @note   Raise the BusManager destroying (0) event.
		 */
		virtual ~BusRingQt();


/** @name Bus interface - management
 * @{
 ******************************************************************************/
	public:

		virtual WRESULT startBus( unsigned long timeout = LIMIT_ULONG_MAX );

		virtual WRESULT stopBus( unsigned long timeout = LIMIT_ULONG_MAX );

		virtual bool isBusRunning() const;

//@}
/** @name Bus interface - messages
 * @{
 ******************************************************************************/
	public:

		virtual void postMessage( Message* message ) const;

		virtual void flushMessages() { }

		virtual int getQueueSize() const					{ return 0; }

//@}
/** @name Bus interface - handlers
 * @{
 ******************************************************************************/
	public:

		virtual WRESULT addBusListener( IBusListener* node );

		virtual WRESULT removeBusListener( IBusListener* node );

		virtual bool isBusListener( const IBusListener* node ) const;

		virtual int countBusListeners() const;

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		virtual void timerEvent( QTimerEvent* timer_event );

		/**
		 * @brief  Post a message on the Bus (forward message to handlers).
		 * @param [in] message the message to be forwarded to handlers
		 * @see    
		 */
		virtual void forward_message_to_listeners( const Message* message );

		virtual void forward_event_to_listeners( Bus::BUS_STATE state );

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		int timerID;
		mutable MessageQueue msgQueue;

		std::vector<IBusListener*> nodes;	///< Listeners of the Bus
		MutexRW nodesMux;					///< mutex for Bus::nodes

		friend class wosh::BusManager;			///< let our 'manager' access protected functions

//@}



}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Core_BusRingQt_H__
